package org.javaboy.meeting.service;

import org.javaboy.meeting.mapper.EmployeeMapper;
import org.javaboy.meeting.mapper.MeetingMapper;
import org.javaboy.meeting.model.Employee;
import org.javaboy.meeting.model.Meeting;
import org.javaboy.meeting.model.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MeetingService {

    @Autowired
    MeetingMapper meetingMapper;

    @Autowired
    EmployeeMapper employeeMapper;

    public Integer addMeeting(Meeting meeting, Integer[] mps) {

       Employee emp = employeeMapper.getEmpByEmpId(meeting.getReservationistid());

       meeting.setReservername(emp.getEmployeename());
        meeting.setReservationtime(new Date());
        Integer result = meetingMapper.addMeeting(meeting);
        meetingMapper.addParticipants(meeting.getMeetingid(), mps);
        return result;
    }

    public List<Meeting> getAllMeetings(Meeting meeting, Integer page, Integer pageSize) {
        page = (page - 1) * pageSize;
        return meetingMapper.getAllMeetings(meeting,page,pageSize);
    }

    public Long getTotal(Meeting meeting) {
        return meetingMapper.getTotal(meeting);
    }

    public Meeting getMeetingById(Integer meetingid) {
       return meetingMapper.getMeetingById(meetingid);
    }

    public List<Meeting> getMyMeetingByEmpId(Integer employeeid) {
        return meetingMapper.getMyMeetingByEmpId(employeeid);
    }

    public List<Meeting> getMyBookingByEmpId(Integer employeeid,Integer status) {
        return meetingMapper.getMyBookingByEmpId(employeeid,status);
    }

    public List<Meeting> getCancelMeetingsByStatus(Integer cancelStatus) {
        return meetingMapper.getCancelMeetingsByStatus(cancelStatus);
    }

    //分页新增
    public Pager<Meeting> getMeetingsbyPage(Integer start, Integer size){
        Pager<Meeting> pager = new Pager<Meeting>();
        List<Meeting> meetings= meetingMapper.getMeetingsbyPage(start,size);
        Integer count = meetingMapper.getCount();
        pager.setList(meetings);
        pager.setRecordCount(count);//返回总记录数
        pager.setPageStart(start);//返回这一页开始项
        pager.setPageCount(count%10 == 0 ? count/10 : count/10+1);
        pager.setPageLastStart((pager.getPageCount()-1)*10);
        if((start-10)>0){
            pager.setPreviousStart(start-10);
        }else{
            pager.setPreviousStart(0);
        }
        if((start+10)<count){
            pager.setNextStart(start+10);
        }else{
            pager.setNextStart(start);
        }
        return pager;
    }

    public Pager<Meeting> searchMeetingsbyPage(int start,Integer size,String meetingname,String roomid,String reservername){
        Pager<Meeting> pager = new Pager<Meeting>();
        List<Meeting> meetings= meetingMapper.searchMeetingsbyPage(start,size,meetingname,roomid,reservername);
        Integer count = meetingMapper.getSearchCount(meetingname,roomid,reservername);
        pager.setList(meetings);
        pager.setRecordCount(count);//返回总记录数
        pager.setPageStart(start);//返回这一页开始项
        pager.setPageCount(count%10 == 0 ? count/10 : count/10+1);
        pager.setPageLastStart((pager.getPageCount()-1)*10);
        if((start-10)>0){
            pager.setPreviousStart(start-10);
        }else{
            pager.setPreviousStart(0);
        }
        if((start+10)<count){
            pager.setNextStart(start+10);
        }else{
            pager.setNextStart(start);
        }
        return pager;
    }

    public List<Meeting> getMeetingByRoomId(Integer roomid) {
        return meetingMapper.getMeetingByRoomId(roomid);
    }
}
